<template>
   <ex-dialog v-model="visible" draggable :title="form.name ? form.name : (form.id ? form.name : '新增') "
  width="700" :contentHeight="450">
    <template #default>
      <el-tabs v-model="activeName" style="height: 100%">
        <el-tab-pane label="基本信息" name="base" style="height: 100%">
          <el-row :gutter="80">
            <el-col :lg="24">
              <el-form :model="form" :rules="button_rules" ref="dialogForm" label-width="80px" label-position="left">
                <el-form-item label="数据类型" prop="isGroup">
                  <ex-radio-group v-if="!form.id" v-model="form.isGroup" dict-code="IS_GROUP_DOCUMENT_CONFIG" button></ex-radio-group>
                  <ex-label v-else code="IS_GROUP_DOCUMENT_CONFIG" :value="form.isGroup"></ex-label>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                  <el-input v-model="form.name" clearable placeholder="名称"></el-input>
                </el-form-item>
                <el-form-item label="编码" prop="code">
                  <el-input v-model="form.code" clearable placeholder="编码"></el-input>
                </el-form-item>
                <el-form-item label="归属组" prop="parentLevelCode">
                  <ex-cascader v-model="form.parentLevelCode" dict-code="FamsDocumentConfig" key-prop="levelCode"
                               placeholder="归属组" :params="{isGroup:'1'}"></ex-cascader>
                </el-form-item>
                <template v-if="form.isGroup==='0'">
                  <el-form-item label="单据类型" prop="type">
                    <ex-select v-model="form.type" dict-code="DOCUMENT_TYPE" placeholder="请选择单据类型"></ex-select>
                  </el-form-item>
                  <el-form-item label="单据范围" prop="manageRangeType">
                    <ex-select v-model="form.manageRangeType" dict-code="DOCUMENT_MANAGE_RANGE_TYPE"
                               placeholder="请选择单据管理范围"></ex-select>
                  </el-form-item>
                  <el-form-item label="查询卡片" prop="queryCardId">
                    <ex-cascader v-model="form.queryCardId" dict-code="FamsAssetCard" placeholder="单据查询卡片"
                                 :check-strictly="false"></ex-cascader>
                    <!--					  <el-cascader v-model="form.queryCardId" :options="cardOptions" :props="cardProps"-->
                    <!--								   :show-all-levels="false" placeholder="单据查询卡片" clearable filterable></el-cascader>-->
                  </el-form-item>
                  <!--                <el-form-item label="大类编码" prop="mainCategoryCode">-->
                  <!--                  <ex-select v-model="form.mainCategoryCode" dict-code="MAIN_CATEGORY"-->
                  <!--                             placeholder="请选择大类编码"></ex-select>-->
                  <!--                </el-form-item>-->
                </template>
                <el-form-item label="备注" prop="description">
                  <el-input v-model="form.description" clearable placeholder="备注"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="orderWeight">
                  <el-input v-model="form.orderWeight" clearable placeholder="排序"></el-input>
                </el-form-item>
              </el-form>

            </el-col>
          </el-row>
        </el-tab-pane>
<!--        <el-tab-pane v-if="form.isGroup==='0'" label="资产类别" name="AssetCategory" style="height: 100%">-->
<!--          <ex-rel-table ref="categoryRelTable" size="small" :rel-entity="form"-->
<!--                        :treeApi="this.$API.asset.assetcategory.treeData" default-expand-all></ex-rel-table>-->
<!--        </el-tab-pane>-->
      </el-tabs>
    </template>
    <template #footer>
      <div style="flex: auto;text-align: center">
        <el-button plain type="primary" @click="save" :loading="loading">保 存</el-button>
        <el-button plain type="warning" @click="reset" :loading="loading">重 置</el-button>
        <el-button plain type="info" @click="this.visible = false" :loading="loading">关 闭</el-button>
      </div>
    </template>

  </ex-dialog>
</template>

<script>
import scIconSelect from '@/components/scIconSelect'
import ExRelTable from "@/components/exRelTable/index.vue";
import ExRadioGroup from "@/components/exRadioGroup/index.vue";
import ExSelect from "@/components/exSelect/index.vue";
import ExCascader from "@/components/exCascader/index.vue";
import ExLabel from "@/components/exLabel/index.vue";
import exDialog from '@/components/exDialog';
import BaseTemplate from "@/utils/baseTemplate";

export default {
  computed: {},
  emits: ['success'],
  components: {
    ExLabel,
    ExCascader,
    ExSelect,
    ExRadioGroup,
    ExRelTable,
    scIconSelect,
    exDialog
  },
  props: {
    documentconfig: {
      type: Object, default: () => {
      }
    },
  },
  data() {
    return {
      activeName: 'base',
      visible: false,
      form: {
        id: "",
        code: "",
        name: "",
        parentLevelCode: "",
        isGroup: "",
        description: "",
      },
      documentconfigOptions: [],
      documentconfigProps: {
        value: 'levelCode',
        label: 'label',
        checkStrictly: true,
        emitPath: false,
      },
      assetCategoryOptions: [],
      assetCategoryProps: {
        value: 'id',
        label: 'label',
        checkStrictly: true,
        emitPath: false,
      },
      cardOptions: [],
      cardProps: {
        value: 'id',
        label: 'label',
        checkStrictly: false,
        emitPath: false,
      },
      button_rules: {
        isGroup: [
          {required: true, message: "请选择数据类型", trigger: "change"},
        ],
        name: [
          {required: true, message: "请输入方式名称", trigger: "change"},
        ],

      },
      loading: false,
      oldForm: null,
    }
  },
  watch: {},
  mounted() {

  },
  methods: {
    async initData() {
      //this.documentconfigOptions = await this.$cache.tree.options(this.$API.asset.documentConfig.treeData);
      // this.documentconfigOptions = await this.$cache.tree.options(this.$API.asset.documentConfig.treeData.storeName, {parentLevelCode: "0000000"});
      // this.assetCategoryOptions = await this.$cache.tree.options(this.$API.asset.assetcategory.treeData.storeName, {});
      // this.cardOptions = await this.$cache.tree.options(this.$API.asset.card.treeData.storeName, {parentLevelCode: "0000000"});
    },
    relHandler(relMap) {
      if (!this.form.relMaps) {
        this.form.relMaps = {};
      }
      for (let key in relMap) {//清空旧的数据
        this.form.relMaps[key] = {};
      }
      //重新赋值新数据
      Object.assign(this.form.relMaps, relMap);
    },
    //显示
    open(entity) {
      this.form = Object.assign({}, entity);
      this.oldForm = Object.assign({}, this.form);
      this.visible = true;
      this.activeName = "base";
      this.initData();
    },
    reset() {
      this.form = Object.assign({}, this.oldForm);
      for (let ref in this.$refs) {
        try {
          this.$refs[ref].refresh();
        } catch (e) {
        }
      }
    },
    //保存
    async save() {
      let valid = await BaseTemplate.validate(this,this.$refs.dialogForm);
      if (valid) {
        try {
          this.loading = true
          var res;
          if (this.form.id) {
            res = await this.$API.asset.documentConfig.edit.post(this.form)
          } else {
            res = await this.$API.asset.documentConfig.add.post(this.form)
          }
          if (res.code === 2000) {

            this.$message.success(this.$t("code." + res.code));
            this.$emit('success');
            this.visible = false;
            this.$store.dispatch("commit", this.form.entityName);
          } else {
            this.$message.warning(res.message)
          }
        } finally {
          this.loading = false
        }
      }
    },
  }
}
</script>

<style scoped>
h2 {
  font-size: 17px;
  color: #3c4a54;
  padding: 0 0 30px 0;
}

[data-theme="dark"] h2 {
  color: #fff;
}

</style>
